#include "contactListWidget.h"

contactListWidget::contactListWidget(QWidget *parent, contactData *data):QListWidget(parent)
{
	mContactData = data;
	mContactData->loadContactXml();
	updateList();
	connect(this,SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotItemClicked(QListWidgetItem *)));
}

contactListWidget::~contactListWidget()
{
	mContactData->saveContactXml();
}

void contactListWidget::updateList()
{
	this->clear();
	QListWidgetItem *item = new QListWidgetItem(cntr("创建联系人"), this);
	this->addItem(item);
	//将myList中的项目添加到列表中
	for(int i=0; i<mContactData->myList.count(); i++)
	{
		QListWidgetItem *item = new QListWidgetItem(mContactData->myList[i].name + "\t" + mContactData->myList[i].phoneNum, this);
		this->addItem(item);
	}
	for(int i=0; i<mContactData->myListBlack.count(); i++)
	{
		QListWidgetItem *item = new QListWidgetItem(mContactData->myListBlack[i].name + "\t" + mContactData->myListBlack[i].phoneNum, this);
		item->setTextColor(QColor(255,0,0));
		this->addItem(item);
	}
}
void contactListWidget::addContact()
{
	addContactDialog *dalg = new addContactDialog();
	QDialog *errordalg = new QDialog();
	dalg->exec();
	if(dalg->result() == 0) 
	{
		delete errordalg;
		delete dalg;
		return;
	}

	contact tmpData;
	tmpData.name = dalg->editName->text();
	tmpData.phoneNum = dalg->editPhone->text();
	Qt::CheckState sta = dalg->blackCheckBox->checkState();
	if(sta == Qt::Unchecked)
		tmpData.isBlack = false;
	else
		tmpData.isBlack = true;

		for(int i=0; i<mContactData->myList.count(); i++)
		{
			if(mContactData->myList[i].name == tmpData.name)
			{
				errordalg->setWindowTitle(cntr("添加失败"));
				QVBoxLayout *mainVBoxLayout = new QVBoxLayout(errordalg);
				QLabel *errorLabel = new QLabel(cntr("联系人已经存在"), errordalg);
				mainVBoxLayout->addWidget(errorLabel);
				errordalg->exec();
				delete errordalg;
				delete dalg;
				return;
			}
		}
		for(int i=0; i<mContactData->myListBlack.count(); i++)
		{
			if(mContactData->myListBlack[i].name == tmpData.name)
			{
				errordalg->setWindowTitle(cntr("添加失败"));
				QVBoxLayout *mainVBoxLayout = new QVBoxLayout(errordalg);
				QLabel *errorLabel = new QLabel(cntr("联系人已经存在"), errordalg);
				mainVBoxLayout->addWidget(errorLabel);
				errordalg->exec();
				delete errordalg;
				delete dalg;
				return;
			}
		}
	if(tmpData.isAvailable())
	{
		if(tmpData.isBlack)
			mContactData->myListBlack.push_back(tmpData);
		else
			mContactData->myList.push_back(tmpData);
	}
	updateList();
	delete errordalg;
	delete dalg;
	emit contactDataChanged();
}

void contactListWidget::slotItemClicked(QListWidgetItem *item)
{
	int row = this->currentRow();
	if(row == 0)//增加联系人
	{
		addContact();
		return;
	}
	bool isBlack;
	contactList *list;
	if(row > mContactData->myList.count())
	{
		row -= (mContactData->myList.count() + 1);
		isBlack = true;
		list = &mContactData->myListBlack;
	}else{
		row--;
		isBlack = false;
		list = &mContactData->myList;
	}
	controlContactDialog *dalg = new controlContactDialog();
		dalg->setContext((*list)[row].name, (*list)[row].phoneNum, isBlack);
	dalg->exec();
	switch(dalg->controlContactSta)
	{
		case controlContactDialog::del:
			list->removeAt(row);
			break;
		case controlContactDialog::save:
			(*list)[row].name = dalg->editName->text();
			(*list)[row].phoneNum = dalg->editPhone->text();
			if(dalg->blackCheckBox->checkState() == Qt::Unchecked && isBlack)//如果编辑了黑名单，而且取消了黑名单
			{
				(*list)[row].isBlack = false;
				mContactData->moveFromBlack(row);//则将它更新后，移动到白名单
			}
			else if(dalg->blackCheckBox->checkState() == Qt::Checked && !isBlack)//如果编辑了白名单，而且转换成了黑名单，则移动到黑名单
			{
				(*list)[row].isBlack = true;	
				mContactData->moveToBlack(row);
			}
			break;
		case controlContactDialog::send:
			//发送短信，进入发送界面
			printf("此功能尚未完成\n");
			break;
		default:
			break;
	}
	updateList();
	delete dalg;
	emit contactDataChanged();
}

